package leetcode_二叉树;

import general_class.TreeNode;

/**
 * 二叉树的直径
 * 算法：递归
 * 求出当前点左子树和右子树的深度, 选择最大的左子树深度 + 右子树深度 的值
 *
 * @author yezh
 * @date 2024/1/13 21:57
 */
public class _543 {

    int ans;

    public int diameterOfBinaryTree(TreeNode root) {
        ans = 0;
        if (root != null) dfs(root);
        return ans;
    }

    int dfs(TreeNode node) {
        int left = 0, right = 0;
        if (node.left != null) left = dfs(node.left);
        if (node.right != null) right = dfs(node.right);
        ans = Math.max(ans, left + right);
        return Math.max(left, right) + 1;
    }

}
